package com.liit.front.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.front.filter.WebContext;
import com.liit.module.entity.Dict;
import com.liit.module.entity.Files;
import com.liit.module.entity.Retail;
import com.liit.module.entity.RetailArea;
import com.liit.module.frame.utils.Const;
import com.liit.module.frame.utils.FileUtil;
import com.liit.module.frame.utils.page.Pagination;
import com.liit.module.service.DictService;
import com.liit.module.service.FilesService;
import com.liit.module.service.RetailAreaService;
import com.liit.module.service.RetailService;

/**
 * @author dingweibo
 * @Title: RetailController.java
 * @Description: 授权门市
 * @date 2015年12月7日
 */
@Controller
@RequestMapping(value = "/retail")
public class RetailController {

	@Resource
	RetailAreaService retailAreaService;

	@Resource
	DictService dictService;

	@Resource
	RetailService retailService;

	@Resource
	FilesService filesService;

	/**
	 * 授权店
	 * 
	 * @return
	 */
	@RequestMapping(value = "/retailList")
	public String retailList() {
		List<Dict> list = dictService.selectByColumn(Const.RETAIL_REGION);
		if (!list.isEmpty()) {
			Dict dict = list.get(0);
			if (dict != null) {
				WebContext.setAttribute("list", list);

				// 查询城市
				List<RetailArea> areaList = retailAreaService.selectAreaByRegion(dict.getValue(), Const.STATUS_ENABLED);
				WebContext.setAttribute("areaList", areaList);

				if (!areaList.isEmpty()) {
					RetailArea retailArea = areaList.get(0);
					if (retailArea != null) {
						// 分页查询门店
						Retail retail = new Retail();
						retail.setArea(retailArea.getId());
						Map<String, Object> result = retailService.listPage(new Pagination(), retail);
						List<Retail> retailList = (List<Retail>) result.get("list");
						WebContext.setAttribute("retailList", retailList);
						WebContext.setAttribute(Const.PAGINATION, result.get("page"));
					}
				}
			}
		}
		IndexController.navigation();
		return "/retail/retailList";
	}

	@RequestMapping(value = "/retailItem/{id}")
	public String retailItem(@PathVariable Integer id) {
		if (id == null) {
			throw new NullPointerException("未找到门店信息");
		}
		Retail retail = retailService.selectById(id);
		if (retail == null) {
			throw new NullPointerException("未找到门店信息");
		}
		if (retail.getImgOne() != null) {
			Files files = filesService.selectById(retail.getImgOne());
			WebContext.setAttribute("imgOne", FileUtil.getUrl(files));
		}
		if (retail.getImgTwo() != null) {
			Files files = filesService.selectById(retail.getImgTwo());
			WebContext.setAttribute("imgTwo", FileUtil.getUrl(files));
		}
		if (retail.getImgThree() != null) {
			Files files = filesService.selectById(retail.getImgThree());
			WebContext.setAttribute("imgThree", FileUtil.getUrl(files));
		}
		WebContext.setAttribute("retail", retail);
		IndexController.navigation();
		return "/retail/retailItem";
	}

	@RequestMapping(value = "/getArea/{value}")
	@ResponseBody
	public Object getArea(@PathVariable("value") Integer value) {
		if (value == null)
			return null;
		return retailAreaService.selectAreaByRegion(value, Const.STATUS_ENABLED);
	}

	@RequestMapping(value = "/getRetail/{id}")
	@ResponseBody
	public Object getRetail(@PathVariable("id") Integer id) {
		if (id == null)
			return null;
		Retail retail = new Retail();
		retail.setArea(id);
		Pagination pagination = new Pagination();
		pagination.setRows(100);
		return retailService.listPage(pagination, retail).get("list");
	}
}
